package com.ygxsk.carhome.ui.adapter;

import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.ygxsk.carhome.R;
import com.ygxsk.carhome.listener.ClickJumpToOrderDetailListener;
import com.ygxsk.carhome.listener.DoubleClickListener;
import com.ygxsk.carhome.response.MyOrderBean;
import com.ygxsk.carhome.response.ShopCartBean;
import com.ygxsk.carhome.sys.Session;
import com.ygxsk.carhome.utils.CommUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by itchenqi on 2018/2/26 0026.
 * description：订单大列表和订单详情内部外侧的大列表适配器，只不过订单详情内部是一条数据的列表项。
 */
public class OrderTransAdapter extends BaseQuickAdapter<MyOrderBean, BaseViewHolder> {

    private Session mSession;
    private ClickJumpToOrderDetailListener clickJumpToOrderDetailListener;

    public OrderTransAdapter(int layoutResId, @Nullable List<MyOrderBean> data) {
        super(layoutResId, data);
        mSession = Session.get(getContext());
    }

    /**
     * 设置点击事件的监听，为了保证外侧适配器和内侧adapter适配器同时
     */
    public void setOnClickJumpToOrderDetailListener(ClickJumpToOrderDetailListener _clickJumpToOrderDetailListener) {
        clickJumpToOrderDetailListener = _clickJumpToOrderDetailListener;
    }

    @Override
    protected void convert(BaseViewHolder helper, final MyOrderBean item) {

        //填充数据内容。。。
        String orderNumber = item.getOrderNo();
        String stateName = item.getOrderStateInfo();//状态的文本信息，另外还有状态的数字标识。

        ArrayList<ShopCartBean> detailList = item.getOrderDetails();

        int count = detailList.size();

        //2018年04月24日修改为应付金额内容
        //BigDecimal amount = item.getTotalAmount();//这个是商品的总价格，因为使用了优惠券所以这个不对
        BigDecimal amount = item.getFinalAmount();
        String yangjiao = mSession.getCurrencySymbol();

        BigDecimal amountformat;
        if (amount != null) {
            amountformat = CommUtils.formatComma2BigDecimal(amount);
        } else {
            amountformat = CommUtils.valueOf(0.00);
        }

        //LinearLayout layoutransdapterout = (LinearLayout)helper.getView(R.id.layoutransdapterout);

        RecyclerView recyclerView = (RecyclerView) helper.getView(R.id.recyclerViewinorder);
        //填充列表的适配器内容
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        //filldata
        //订单编号外侧的按钮也要点击：
        //RelativeLayout relativeLayoutAdapterTop = (RelativeLayout) helper.getView(R.id.relativelayoutadaptertop);//

        helper.setText(R.id.textViewTime, "订单编号：" + (TextUtils.isEmpty(orderNumber) ? "" : orderNumber));//订单编号内容
        helper.setText(R.id.textViewState, TextUtils.isEmpty(stateName) ? "" : stateName);//订单状态

        helper.setText(R.id.textNubmerTotal, "共" + count + "件商品");//订单购买数量。
        helper.setText(R.id.textPriceTotal, yangjiao + amountformat.toString());//总共的价钱

        //helper.addOnClickListener(R.id.layoutransdapterout);

        /*if (relativeLayoutAdapterTop!=null){

            relativeLayoutAdapterTop.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    //注意：和下边的一段代码类似，也是要点击进入详情的
                    if (!DoubleClickListener.isFastDoubleClick()) {

                        if (item != null) {
                            LogUtils.d("itchen----点击顶部进入详情。");
                            Bundle bundle = new Bundle();
                            bundle.putString(ConstantApi.EXTRA_ORDER_NUMBER, item.getOrderNo());
                            ActivityUtils.startActivity(bundle, OrderDetailActivity.class);
                        }

                    }

                }
            });

        }*/

        //添加横线内容 白色横线，目前仅仅有一条商品可以不添加。
        //recyclerView.addItemDecoration(new DividerItemDecorationInAdapter(mContext));

        OrderInsideAdapter accountPayAdapter = new OrderInsideAdapter(R.layout.orderinsideadapter, detailList);
        recyclerView.setAdapter(accountPayAdapter);//设置适配器
        accountPayAdapter.notifyDataSetChanged();

        //这个布局会拦截 onitem的点击事件所有内容
        /*layoutransdapterout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CommUtils.displayToastShort(mContext,"测试点击测试点击");
            }
        });*/

        //2018年02月27日。
        //这里的recyclerview抢占了焦点，导致点击列表无法进入商品详情。

        if (accountPayAdapter != null) {

            //将点击事件添加到内部列表上
            accountPayAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {

                    //CommUtils.displayToastShort(mContext,"测试点击测试点击");
                    //直接从这里点击进入详情
                    if (!DoubleClickListener.isFastDoubleClick()) {

                        if (item != null) {

                           /* Bundle bundle = new Bundle();
                            bundle.putString(ConstantApi.EXTRA_ORDER_NUMBER, item.getOrderNo());
                            ActivityUtils.startActivity(bundle, OrderDetailActivity.class);*/

                            //如上代码也是正确的，但是要和外侧adapter保持一致
                            if (clickJumpToOrderDetailListener != null) {

                                //LogUtils.d("itchen----点击内侧适配器，进入详情。");
                                clickJumpToOrderDetailListener.clickJumpToOrderDetail(item);

                            }
                        }
                    }

                }
            });

        }

    }

    /**
     *@date 2018/2/26 0026
     @auther itchenqi
     @description 订单列表内部的商品列表内部的横线内容, 暂时关闭以后可能重新启用。
     */
    /*class DividerItemDecorationInAdapter extends Y_DividerItemDecoration {

        public DividerItemDecorationInAdapter(Context context) {
            super(context);
        }

        @Override
        public Y_Divider getDivider(int itemPosition) {

            Y_Divider divider = new Y_DividerBuilder()
                    .setBottomSideLine(true, 0xffffff, 6, 0, 0)
                    .create();

            return divider;
        }
    }*/

}
